package com.sxd.bill;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hankcs.hanlp.model.crf.CRFPOSTagger;
import com.hankcs.hanlp.model.crf.CRFSegmenter;
import com.hankcs.hanlp.model.hmm.HMMPOSTagger;
import com.hankcs.hanlp.model.hmm.HMMSegmenter;
import com.hankcs.hanlp.model.perceptron.PerceptronPOSTagger;
import com.hankcs.hanlp.model.perceptron.PerceptronSegmenter;
import com.hankcs.hanlp.tokenizer.lexical.AbstractLexicalAnalyzer;
import com.hankcs.hanlp.tokenizer.lexical.LexicalAnalyzer;
import com.sxd.bill.common.item.Pairs;
import com.sxd.bill.common.util.JsonUtil;
import com.sxd.bill.ner.utils.WordUtitly;

import lombok.Data;

public class Processor {
    private static String evalPath = "data\\ner\\eval.json";
    private static String trainPath = "data\\ner\\train.json";

    public static List<Pairs<String, String[]>> loadJsonFile(String path) {
        String jsonContent;
        try {
            jsonContent = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            jsonContent = null;
        }
        JSONArray jsonArray = JSON.parseArray(jsonContent);
        List<Pairs<String, String[]>> items = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String text = jsonObject.getString("text");
            String tag = jsonObject.getString("tag");
            String[] tags = tag.split(" ");
            Pairs<String, String[]> pairs = new Pairs<String, String[]>(text, tags);
            items.add(pairs);
        }
        return items;
    }

    public static void saveBIOSData(List<Item> data, String path) {
        try {
            FileOutputStream fos = new FileOutputStream(path);
            OutputStreamWriter writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
            // FileWriter writer = new FileWriter(path);
            for (Item item : data) {
                String text = item.getText();
                String[] tags = item.getTags();
                String[] cates = item.getKinds();
                for (int i = 0; i < text.length(); i++) {
                    String word = text.substring(i, i + 1);
                    String w;
                    if (word.trim().length() == 0) {
                        w = "\f";
                    } else {
                        w = word;
                    }
                    String cate = cates[i];
                    String tag = tags[i];
                    String line = String.join("\t", new String[] { w, cate, tag });
                    writer.write(line + "\n");
                }
                writer.write("\n");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        // LexicalAnalyzer analyzer = new AbstractLexicalAnalyzer(new CRFSegmenter(),
        // new CRFPOSTagger());
        // LexicalAnalyzer analyzer = new AbstractLexicalAnalyzer(new
        // PerceptronSegmenter(), new PerceptronPOSTagger());

        // String text = "同创合鑫 赵达超15215707830 : 收跨年重庆 青岛农 河北 成都 天津 九江 江西 郑州 中原 稠州
        // 宁波通商杭州联合农 萧山农 江南农 等优质城农 城贴以上可自贴\n收1月遂宁 或者2月初到期 非村贴";
        // System.out.println(analyzer.analyze(text));

        List<Pairs<String, String[]>> pairs = loadJsonFile(trainPath);
        List<Item> items = new ArrayList<>();
        int j = 0;
        for (Pairs<String, String[]> pair : pairs) {
            String text = pair.getFirst();
            String[] tags = pair.getSecond();
            if (text.length() != tags.length) {
                j++;
                continue;
            }
            String[] kinds = WordUtitly.getCharKind(text);
            Item item = new Item();
            item.setKinds(kinds);
            item.setTags(tags);
            item.setText(text);
            items.add(item);
        }
        System.out.println(j);
        JsonUtil.saveObjectAsJson(items, "output/aaa.json");
        saveBIOSData(items, "data/ner/train.txt");

    }
}

@Data
class Item {
    private String text;
    private String[] tags;
    private String[] kinds;
    private List<String> segs;

    public Item() {
        this.segs = new ArrayList<>();
    }

    public void addSegs(String seg, int cnt) {
        for (int i = 0; i < cnt; i++) {
            segs.add(seg);
        }
    }

}
